package com.hefei.customer.info.controller;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fasterxml.jackson.databind.JsonNode;
import com.hefei.common.http.response.BaseResponse;
import com.hefei.common.returncode.ReturnCode;
import com.hefei.common.util.JacksonUtil;
import com.hefei.customer.info.po.Customer;
import com.hefei.customer.info.service.ICustomerService;
import com.hefei.service.framework.base.request.RequestThreadLocal;
import com.hefei.service.framework.exception.ServiceException;





@Controller
@RequestMapping(value="/customer", produces = "text/plain;charset=UTF-8")
public class CustomerController {
	
	private Logger logger = Logger.getLogger(CustomerController.class);
	@Autowired
	private ICustomerService customerService;
	
	@RequestMapping(value="/addCustomer",produces="text/plain;charset=UTF-8",method=RequestMethod.POST)
	@ResponseBody
	public String saveCustomerInfo(){
		
		BaseResponse result = new BaseResponse();
		result.setReturnCode(ReturnCode.RETURN_CODE_ERROR);
		
		try {
			String plainString = RequestThreadLocal.getPlain();
			if(StringUtils.isBlank(plainString)){
				logger.info(RequestThreadLocal.getLoggerStr() + " Plain is null ");
				result.setReturnCode(ReturnCode.RETURN_CODE_ERROR_PARAM_NULL);
				return JacksonUtil.beanToJson(result);
			}
			
			JsonNode node = JacksonUtil.getJsonNode(plainString);
			JsonNode customerInfo = node.get("customer");
			Customer customer = JacksonUtil.jsonToBean(customerInfo, Customer.class);
			
			if (customer == null) {
				result.setReturnCode(ReturnCode.RETURN_CODE_ERROR_PARAM_NULL);
			}else{
				customerService.saveCustomerInfo(customer);
				result.setReturnCode(ReturnCode.RETURN_CODE_SUCCESS);
			}
			
		} catch(ServiceException e){
			result.setReturnCode(e.getErrorCode());
		} catch (Exception e) {
			result.setReturnCode(ReturnCode.RETURN_CODE_ERROR);
			logger.error(RequestThreadLocal.getLoggerStr()+" saveCustomerInfo error:",e);
		}
		
		String resultStr = JacksonUtil.beanToJson(result);
		logger.info(RequestThreadLocal.getLoggerStr()+" saveCustomerInfo result:"+resultStr);
		return resultStr;
		
	}
	@ResponseBody
	@RequestMapping(value="/updateCustomer",produces="text/plain;charset=UTF-8",method=RequestMethod.POST)
	public String updateCustomerInfo(){
		
		BaseResponse result = new BaseResponse();
		result.setReturnCode(ReturnCode.RETURN_CODE_ERROR);
		try {
			String plainString = RequestThreadLocal.getPlain();
			if(StringUtils.isBlank(plainString)){
				logger.info(RequestThreadLocal.getLoggerStr() + " Plain is null ");
				result.setReturnCode(ReturnCode.RETURN_CODE_ERROR_PARAM_NULL);
				return JacksonUtil.beanToJson(result);
			}
			
			JsonNode node = JacksonUtil.getJsonNode(plainString);
			JsonNode customerInfo = node.get("customer");
			Customer customer = JacksonUtil.jsonToBean(customerInfo, Customer.class);
			
			if (customer == null) {
				result.setReturnCode(ReturnCode.RETURN_CODE_ERROR_PARAM_NULL);
			}else{
				customerService.updateCustomerInfo(customer);
				result.setReturnCode(ReturnCode.RETURN_CODE_SUCCESS);
			}
			
		} catch(ServiceException e){
			result.setReturnCode(e.getErrorCode());
		} catch (Exception e) {
			result.setReturnCode(ReturnCode.RETURN_CODE_ERROR);
			logger.error(RequestThreadLocal.getLoggerStr()+" saveCustomerInfo error:",e);
		}
		
		String resultStr = JacksonUtil.beanToJson(result);
		logger.info(RequestThreadLocal.getLoggerStr()+" saveCustomerInfo result:"+resultStr);
		return resultStr;
	
		
	}
	@ResponseBody
	@RequestMapping(value="/deleteCustomer",produces="text/plain;charset=UTF-8",method=RequestMethod.POST)
	public String deleteCustomerInfo(){
		BaseResponse result = new BaseResponse();
		result.setReturnCode(ReturnCode.RETURN_CODE_ERROR);
		try {
			String plainString = RequestThreadLocal.getPlain();
			if(StringUtils.isBlank(plainString)){
				logger.info(RequestThreadLocal.getLoggerStr() + " Plain is null ");
				result.setReturnCode(ReturnCode.RETURN_CODE_ERROR_PARAM_NULL);
				return JacksonUtil.beanToJson(result);
			}
			
			JsonNode node = JacksonUtil.getJsonNode(plainString);
			Integer id = null;
			if(node.has("id")){
				id = node.get("id").asInt();
			}
			
			if (id == null) {
				result.setReturnCode(ReturnCode.RETURN_CODE_ERROR_PARAM_NULL);
			}else{
				customerService.deleteCustomerInfo(id);
				result.setReturnCode(ReturnCode.RETURN_CODE_SUCCESS);
			}
			
			
		} catch(ServiceException e){
			result.setReturnCode(e.getErrorCode());
		} catch (Exception e) {
			result.setReturnCode(ReturnCode.RETURN_CODE_ERROR);
			logger.error(RequestThreadLocal.getLoggerStr()+" saveCustomerInfo error:",e);
		}
		
		String resultStr = JacksonUtil.beanToJson(result);
		logger.info(RequestThreadLocal.getLoggerStr()+" saveCustomerInfo result:"+resultStr);
		return resultStr;
		
	}
	@ResponseBody
	@RequestMapping(value="/getCustomerById",produces="text/plain;charset=UTF-8",method=RequestMethod.POST)
	public String getCustomerById(){
		
		BaseResponse result = new BaseResponse();
		result.setReturnCode(ReturnCode.RETURN_CODE_ERROR);
		try {
			String plainString = RequestThreadLocal.getPlain();
			if(StringUtils.isBlank(plainString)){
				logger.info(RequestThreadLocal.getLoggerStr() + " Plain is null ");
				result.setReturnCode(ReturnCode.RETURN_CODE_ERROR_PARAM_NULL);
				return JacksonUtil.beanToJson(result);
			}
			
			Integer id = null;
			JsonNode jsonNode = JacksonUtil.jsonToBean(plainString, JsonNode.class);
			if( jsonNode.has("id")) {
				id  = jsonNode.get("id").asInt();
				
			}
			
			if(id == null){
				result.setReturnCode(ReturnCode.RETURN_CODE_ERROR_PARAM_NULL);
			}else{
				Customer customer = customerService.getCustomerById(id);
				if(customer != null){
					result.setReturnCode(ReturnCode.RETURN_CODE_SUCCESS);
					result.setReturnObject(customer);
				}else{
					result.setReturnCode(ReturnCode.RETURN_CODE_ERROR_DATA_NOTEXISTS);
				}
			}
			
		} catch(ServiceException e){
			result.setReturnCode(e.getErrorCode());
		} catch (Exception e) {
			result.setReturnCode(ReturnCode.RETURN_CODE_ERROR);
			logger.error(RequestThreadLocal.getLoggerStr()+" getCustomerById error:",e);
		}
		
		String resultStr = JacksonUtil.beanToJson(result);
		logger.info(RequestThreadLocal.getLoggerStr()+" getCustomerById result:"+resultStr);
		return resultStr ;
		
	}
	@ResponseBody
	@RequestMapping(value="/getCustomerById",produces="text/plain;charset=UTF-8",method=RequestMethod.POST)
	public String getCustomerByName(){
		
		BaseResponse result = new BaseResponse();
		result.setReturnCode(ReturnCode.RETURN_CODE_ERROR);
		try {
			String plainString = RequestThreadLocal.getPlain();
			if(StringUtils.isBlank(plainString)){
				logger.info(RequestThreadLocal.getLoggerStr() + " Plain is null ");
				result.setReturnCode(ReturnCode.RETURN_CODE_ERROR_PARAM_NULL);
				return JacksonUtil.beanToJson(result);
			}
			
			String name = null;
			JsonNode jsonNode = JacksonUtil.jsonToBean(plainString, JsonNode.class);
			if( jsonNode.has("name")) {
				 name  = jsonNode.get("name").asText();
				
			}
			
			if(name == null){
				result.setReturnCode(ReturnCode.RETURN_CODE_ERROR_PARAM_NULL);
			}else{
				Customer customer = customerService.getCustoemrByName(name);
				if(customer != null){
					result.setReturnCode(ReturnCode.RETURN_CODE_SUCCESS);
					result.setReturnObject(customer);
				}else{
					result.setReturnCode(ReturnCode.RETURN_CODE_ERROR_DATA_NOTEXISTS);
				}
			}
			
		} catch(ServiceException e){
			result.setReturnCode(e.getErrorCode());
		} catch (Exception e) {
			result.setReturnCode(ReturnCode.RETURN_CODE_ERROR);
			logger.error(RequestThreadLocal.getLoggerStr()+" getCustomerById error:",e);
		}
		
		String resultStr = JacksonUtil.beanToJson(result);
		logger.info(RequestThreadLocal.getLoggerStr()+" getCustomerById result:"+resultStr);
		return resultStr ;
		
	}
	
	
	
}
